﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows;
using Microsoft.Kinect;
using Microsoft.Samples.Kinect.WpfViewers;

using ProgramController;

namespace ZeroTouchUI
{
    public class Profile
    {
        Dictionary<Gesture, Action> profile;
        List<Action> action_list;
        string name;
        public program_controller pc;

        public Profile()
        {
            profile = new Dictionary<Gesture, Action>();
            action_list = new List<Action>();
            pc = new program_controller();
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public void AddGesture(Gesture g, Action a)
        {
            profile.Add(g, a);
        }

        public void RemoveGesture(Gesture g)
        {
            profile.Remove(g);
        }

        public void AddAction(Action a)
        {
            action_list.Add(a);

            //add it to the program_controller also
            if (a.Type == "1")
            {
                pc.add_event1(a.Name, a.Info, a.CustomData);
            }
            else if (a.Type == "2")
            {
                pc.add_event2(a.Name, a.Info, a.CustomData);
            }
        }

        public void RemoveAction(Action a)
        {
            action_list.Remove(a);
        }

        public List<Action> getActionList()
        {
            return action_list;
        }

        public Dictionary<Gesture, Action> getDictionary()
        {
            return profile;
        }

        public void Clear()
        {
            profile.Clear();
        }

        public Profile LoadProfile(string filename, Profile p)
        {
            if (System.IO.File.Exists(filename + ".xml"))
            {
                p.Clear();
                XmlTextReader reader = new XmlTextReader(filename + ".xml");

                //read whole file
                while (reader.Read())
                {
                    XmlNodeType nType = reader.NodeType;
                    //Console.WriteLine("This node is a: " + nType.ToString());

                    if (nType == XmlNodeType.Element)
                    {
                        Console.WriteLine("ELEMENTNAME: " + reader.Name.ToString());
                        if (reader.Name.ToString() == "Name")
                        {
                            reader.Read();
                            p.name = reader.Value;
                            Console.WriteLine("Profile is now named: " + p.Name);
                        }

                        if (reader.Name.ToString() == "Gesture")
                        {
                            Gesture g = new Gesture();
                            g.leftGesture = new List<Point>();
                            g.rightGesture = new List<Point>();
                            Action a = new Action();
                            bool gestureComplete = false;
                            while (!gestureComplete)
                            {
                                reader.Read();
                                if (reader.Name.ToString() == "GestureName" && reader.NodeType != XmlNodeType.EndElement)
                                {
                                    reader.Read();
                                    g.Name = reader.Value;
                                    Console.WriteLine("New gesture named: " + g.Name);
                                }

                                if (reader.Name.ToString() == "LH" && reader.NodeType != XmlNodeType.EndElement)
                                {
                                    reader.Read();
                                    string data = reader.Value;
                                    int startToComma = data.Substring(0, data.IndexOf(',')).Length + 1;
                                    double x = Double.Parse(data.Substring(0, data.IndexOf(',')));
                                    double y = Double.Parse(data.Substring(data.IndexOf(',') + 1, data.Length - startToComma));
                                    g.leftGesture.Add(new Point(x, y));
                                    Console.WriteLine("Added New LH Point: " + x + ", " + y);
                                }

                                if (reader.Name.ToString() == "RH" && reader.NodeType != XmlNodeType.EndElement)
                                {
                                    reader.Read();
                                    string data = reader.Value;
                                    int startToComma = data.Substring(0, data.IndexOf(',')).Length + 1;
                                    double x = Double.Parse(data.Substring(0, data.IndexOf(',')));
                                    double y = Double.Parse(data.Substring(data.IndexOf(',') + 1, data.Length - startToComma));
                                    g.rightGesture.Add(new Point(x, y));

                                    Console.WriteLine("Added New RH Point: " + x + ", " + y);
                                }

                                if (reader.Name.ToString() == "ActionName" && reader.NodeType != XmlNodeType.EndElement)
                                {
                                    reader.Read();
                                    a.Name = reader.Value;
                                    Console.WriteLine("New action named: " + a.Name); 
                                    gestureComplete = true;
                                }

                                /*if (reader.Name.ToString() == "ActionInfo" && reader.NodeType != XmlNodeType.EndElement)
                                {
                                    reader.Read();
                                    a.Info = reader.Value;
                                    Console.WriteLine("New action info: " + a.Info);
                                }
                                if (reader.Name.ToString() == "ActionCustomData" && reader.NodeType != XmlNodeType.EndElement)
                                {
                                    reader.Read();
                                    a.CustomData = reader.Value;
                                    Console.WriteLine("New action customData: " + a.CustomData);
                                   
                                }*/
                            }
                            p.AddGesture(g, a);
                            Console.WriteLine("Added the pair " + g.Name + ", " + a.Name + " to the profile.");
                        }
                        if (reader.Name.ToString() == "CustomAction")
                        {
                            Console.WriteLine("Found A Custom Action!");
                            Action a = new Action();
                            bool customactionComplete = false;
                            while (!customactionComplete)
                            {
                                reader.Read();
                                if (reader.Name.ToString() == "ActionName" && reader.NodeType != XmlNodeType.EndElement)
                                {
                                    reader.Read();
                                    a.Name = reader.Value;
                                }
                                if (reader.Name.ToString() == "ActionInfo" && reader.NodeType != XmlNodeType.EndElement)
                                {
                                    reader.Read();
                                    a.Info = reader.Value;
                                }
                                if (reader.Name.ToString() == "ActionCustomData" && reader.NodeType != XmlNodeType.EndElement)
                                {
                                    reader.Read();
                                    a.CustomData = reader.Value;
                                }
                                if (reader.Name.ToString() == "ActionType" && reader.NodeType != XmlNodeType.EndElement)
                                {
                                    reader.Read();
                                    a.Type = reader.Value;
                                    customactionComplete = true;
                                }
                            }
                            //action_list.Add(a);
                            Console.WriteLine("Added Custom Action:[" + a.Name + "] [" + a.Info + "] [" + a.CustomData +"]");
                            AddAction(a);
                            // add to action_list
                        }
                    }
                }

                reader.Close();
            }
            return p;
        }

        public void SaveProfile(string filename)
        {
            XmlTextWriter writer = new XmlTextWriter(filename + ".xml", null); // null defaults to UTF-8 encoding.
            //save the profile 

            writer.WriteStartDocument();
            writer.WriteComment("Saved on " + System.DateTime.Now.ToString());
            writer.WriteStartElement("Profile");
            writer.WriteElementString("Name", filename);


            foreach (KeyValuePair<Gesture, Action> pair in profile)
            {
                Gesture g = pair.Key;
                Action a = pair.Value;
                writer.WriteStartElement("Gesture");
                writer.WriteElementString("GestureName", g.Name);
                //Left Hand point information
                foreach (Point p in g.leftGesture)
                {
                    writer.WriteElementString("LH", p.ToString());
                }
                //Right Hand point information
                foreach (Point p in g.rightGesture)
                {
                    writer.WriteElementString("RH", p.ToString());
                }
                writer.WriteElementString("ActionName", a.Name);
                //writer.WriteElementString("ActionInfo", a.Info);//Action information goes here
                //writer.WriteElementString("ActionCustomData", a.CustomData);
                writer.WriteEndElement();
            }
  
            // save all of the actions
            foreach (Action a in action_list)
            {

                writer.WriteStartElement("CustomAction");

                writer.WriteElementString("ActionName", a.Name);
                writer.WriteElementString("ActionInfo", a.Info);//Action information goes here
                writer.WriteElementString("ActionCustomData", a.CustomData);
                writer.WriteElementString("ActionType", (a.Type).ToString());

                writer.WriteEndElement();
            }

            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
            //end saving

        }
    }
}
